using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Web.UI.HtmlControls;
using System.Collections.Generic;

public partial class adm_sc_categories_Default : System.Web.UI.Page
{
    private Category_Struct _category_struct;

    public int PageNumber
    {
        get
        {
            if (ViewState["PageNumber"] != null)
                return (int)ViewState["PageNumber"];
            else
                return 1;
        }
        set
        {
            ViewState["PageNumber"] = value;
        }
    }

    public string Indicator
    {
        get
        {
            if (ViewState["indicator"] != null)
                return ViewState["indicator"].ToString();
            else
                return "";
        }
        set
        {
            ViewState["indicator"] = value;
        }
    }

    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        rptPages.ItemCommand += this.rptPages_ItemCommand;

    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {   
            

            if (Request.Params["t"] != null)
            {
                // set filter record option
                drp_filter.SelectedValue = Request.Params["t"].ToString();
            }
            if (Request.Params["status"] != null)
            {
                switch (Request.Params["status"].ToString())
                {
                    case "deleted":
                        Config.ShowMessage(msg, "Category has been deleted successfully", 1, 2);
                        break;
                    case "added":
                        Config.ShowMessage(msg, "New Category has been added successfully", 1, 2);
                        break;
                    case "updated":
                        Config.ShowMessage(msg, "Category information has been deleted successfully", 1, 2);
                        break;
                }
               
            }
            BindList();
        }
    }
    
    private void BindList()
    {
        this.Indicator = "";
        ArrayList _array = new ArrayList();
        Process_Category_List_Items(_array, 0, int.Parse(drp_filter.SelectedValue));
        if (_array.Count > 0)
        {
            Pagination_Process.Process_Pagination(MyList, rptPages, lnk_Prev, lnk_Next, 20, this.PageNumber, _array);

            pnl_main.Visible = true;
            pnl_norecord.Visible = false;
        }
        else
        {
            pnl_main.Visible = false;
            pnl_norecord.Visible = true;
        }

    }

    // Parent Category Implementation
    // Complete Logic for showing parent child categories
    private void LoadCategories(DropDownList cat,int type)
    {
        cat.Items.Clear();
        ListItem _list = new ListItem();
        _list.Text = "No Parent";
        _list.Value = "0";
        cat.Items.Add(_list);
        // set child level indicator start with empty "", "-", "--", "---", etc
        this.Indicator = "";
        // Recursion Starts //
        CategoriesBLL.Process_Category_Items(cat,0,type,false);
       // Process_Category_Items(cat, 0,type);
    }

    protected void drp_filter_SelectedIndexChanged(object sender, System.EventArgs e)
    {
        // load category detail list
        BindList();
    }
  
    protected void MyList_CancelCommand(object source, DataListCommandEventArgs e)
    {
        MyList.EditItemIndex = -1;
        BindList();
    }

    protected void MyList_DeleteCommand(object source, DataListCommandEventArgs e)
    {
        // Check whether admin section is in readonly mode.
        if (!Config.isAdminActionAllowed())
        {
            Response.Redirect( Config.GetUrl("adm/sc/Default.aspx?action_error=true"));
            return;
        }
        Label lbl_categoryid = (Label)e.Item.FindControl("lbl_categoryid");
        int categoryid = int.Parse(lbl_categoryid.Text);
        string type = ((Label)e.Item.FindControl("lbl_type")).Text;

        Response.Redirect("Delete_Confirmation.aspx?cid=" + categoryid.ToString() + "&type=" + type);
        //CategoriesBLL.Delete(categoryid);
        //Config.ShowMessage(msg, "Category has been deleted successfully.", 1, 2, 300);
       
        //MyList.EditItemIndex = -1;
        //LoadCategories(drp_parent,int.Parse(drp_filter.SelectedValue));
        //BindList();
    }
       
    protected void MyList_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            // delete link
            string categoryid = ((Label)e.Item.FindControl("lbl_categoryid")).Text;
            string type = ((Label)e.Item.FindControl("lbl_type")).Text;
            HyperLink lnk = (HyperLink)e.Item.FindControl("lnk_delete");
            lnk.NavigateUrl = ResolveUrl("Delete_Confirmation.aspx?cid=" + categoryid + "&type=" + type);
            HyperLink edit = (HyperLink)e.Item.FindControl("lnk_edit");
            edit.NavigateUrl = ResolveUrl("Process.aspx?cid=" + categoryid);
        }
    }
   
    protected void lnk_Next_Click(object sender, EventArgs e)
    {
        PageNumber = PageNumber + 1;
        BindList();
    }
    protected void lnk_Prev_Click(object sender, EventArgs e)
    {
        PageNumber = PageNumber - 1;
        BindList();
    }

    protected void rptPages_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        LinkButton btnPage = (LinkButton)e.Item.FindControl("btnPage");
        PageNumber = int.Parse(btnPage.Text);
        BindList();
    }

    protected void rptPages_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            LinkButton btnPage = (LinkButton)e.Item.FindControl("btnPage");
            if (int.Parse(btnPage.Text) == PageNumber)
            {
                btnPage.CssClass = "pagination_link_selected";
            }
            else
            {
                btnPage.CssClass = "pagination_link";
            }
        }

    }


    private void Process_Category_List_Items(ArrayList _arr, int parentid, int type)
    {
        int isprivate = Convert.ToInt32(drp_fstatus.SelectedValue);
        int categoryid = 0; // load all categories
        // load all categories based on parentid
        List<Category_Struct_V2> _list = CategoriesBLL.Load_Categories(txt_search.Text,categoryid,parentid, type, isprivate,drp_order.SelectedValue);
        if (_list.Count > 0)
        {
            int i = 0;
            int _id;
            string _name;
            for (i = 0; i < _list.Count; i++)
            {

                if (parentid == 0)
                    this.Indicator = "";

                if (this.Indicator != "")
                    _name = this.Indicator + "" + _list[i].CategoryName;
                else
                    _name = _list[i].CategoryName;

                _category_struct = new Category_Struct(_list[i].CategoryID, _name, _list[i].Term, parentid, _list[i].Priority, _list[i].Type, this.Indicator, _list[i].isPrivate);
                _arr.Add(_category_struct);
                if (CategoriesBLL.Has_Child(_list[i].CategoryID, isprivate))
                {
                    this.Indicator = this.Indicator + "- ";
                    Process_Category_List_Items(_arr, _list[i].CategoryID, type);
                }
            }

            if (this.Indicator.Trim() != "")
                this.Indicator = this.Indicator.Remove(this.Indicator.LastIndexOf("- "));

            this.Indicator = this.Indicator + " ";

        }
    }

    protected void drp_fstatus_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindList();
    }

    protected void btn_search_Click(object sender, EventArgs e)
    {
        BindList();
    }

    protected void drp_order_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindList();
    }
}
